<?php

require_once(DIR_LIB."VsGui".DIR_SEP."vssmartywidget.php");

require_once(DIR_LIB."Junona".DIR_SEP."junobjectsfactory.php");
require_once(DIR_LIB."Junona".DIR_SEP."junoperatorwork.php");

require_once(dirname(__FILE__).DIR_SEP."vsdateedit.php");

class XCartModelWork extends VsSmartyWidget {

	const DATA_TABLES_DIR = '/DataTables-1.9.4';
	
	function __construct(&$page, $pager=NULL) {
		// Ne go zabravqj ina4e nqma $smarty Object
		parent::__construct();

		$this->_template = dirname(__FILE__).DIR_SEP."xcart_model_work.html";

		$this->_wwwDir = 'templates/XCart/';
		$this->assign('wwwDir', $this->_wwwDir);

		$page->addJsFile("js/jquery-1.7.2.min.js");
		$page->addJsFile("js/jquery-ui-1.8.22.custom.min.js");
		$page->addCssFile("css/ui-lightness/jquery-ui-1.8.22.custom.css");
		
		if(isset($_POST['date'])) {
			$url = $_SERVER['PHP_SELF']."?modid=".$_GET['modid']."&date=".$_POST['date'];
			header("Location: ".$url);
		}
		
		
		if(isset($_POST['saveForm'])) {
			$this->procForm();
			
			header("Location: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
		}
		
		
		$vsdeDate = isset($_GET['date']) ? $_GET['date'] : date("Y-m-d");
		
		$dateEdit = new VsDateEdit($page, $vsdeDate);
		$this->assign('dateEdit', $dateEdit->show());
		
		$this->assign('vsdeDate', $dateEdit->date());
		
		
		
		$model = JunObjectsFactory::makeModel($_REQUEST['modid']);
		$operations = JunObjectsFactory::makeOperations($_REQUEST['modid']);
		$operators = JunObjectsFactory::makeOperatorsPairs();
		$operatorsWork = JunObjectsFactory::makeOperationsWorkCount($_REQUEST['modid'], $dateEdit->date(), $dateEdit->date(), false);
		
		$workCount = array();
		foreach($operatorsWork[0] as $op)  {
			if(!isset($workCount[$op->operators_id])) {
				$workCount[$op->operators_id] = array();
			}
			
			$workId = $op->operators_id . '_' . $dateEdit->date() . '_' . $op->id . '_' . $op->operations_id;
			
			if(!isset($workCount[$op->operators_id][$op->operations_id])) {
				$workCount[$op->operators_id][$op->operations_id] = 
							array('workCount' => 0,
									'workId' => $workId);
			}
			
			$workCount[$op->operators_id][$op->operations_id]['workCount'] += $op->count;
		}
		//echo "<pre>"; die(var_dump($workCount));
		//echo "<pre>"; die(var_dump($operations[0]));
		
		
		$this->assign('model', $model);
		$this->assign('operators', $operators);
		$this->assign('listOperations', $operations[0]);
		$this->assign('operationsTotalCount', $operatorsWork[2]);
		
		$this->assign('modelTotalPrice', $operations[1]);
		$this->assign('modelTotalMinutes', $operations[2]);
		$this->assign('workCount', $workCount);
	}
	//============================================================================

	function show() {

		return $this->fetch($this->_template);
	}
	//============================================================================

	private function procForm() {
		
		/*
		 array(4) {
		  ["model"]=>
		  string(3) "580"
		  ["operation"]=>
		  string(5) "29694"
		  ["price"]=>
		  string(1) "4"
		  ["count"]=>
		  string(2) "10"
		}
		 */
			
		foreach($_POST['workCount'] as $operatorId => $operation) {
			foreach($operation as $operationId => $data) {
				if($data['workCount'] == $data['workCountOld'])
					continue;
				
				$pData = array(
						  "model" => $_POST['modelId'],
						  "operation" => $operationId,
						  "price" => $data['price'],
						  "count" => $data['workCount']
						);
						  
				
				if(empty($data['workId'])) {
					$workId = $operatorId . '_' . $_POST['date'] . '_' . 0 . '_' . $operationId;
					JunOperatorWork::insertRow($workId, $pData);
				}
				else {
					JunOperatorWork::updateRow($data['workId'], $pData);
				}
				
			}
		}
	}
	//===========================================================================

	

	

};

?>
